package com.zxt.dental_sys.Controller.Case;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zxt.dental_sys.model.CaseInfo;
import com.zxt.dental_sys.model.vo.RestBean;
import com.zxt.dental_sys.service.impl.CaseInfoServiceImpl;
import com.zxt.dental_sys.service.impl.SysElementUserinfoServiceImpl;
import com.zxt.dental_sys.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/case")
public class CaseController {

    @Resource
    CaseInfoServiceImpl caseInfoService;

    @Resource
    SysElementUserinfoServiceImpl userinfoService;

    @Resource
    JwtUtil jwtUtil;

    @RequestMapping("/list/{page}/{size}")
    public RestBean queryList(@PathVariable int page,@PathVariable int size){
        PageMethod.startPage(page,size);
        List<CaseInfo> list = caseInfoService.list(new QueryWrapper<CaseInfo>()
                .eq("is_deleted", 0)
                .orderByDesc("input_time")
        );
        list.forEach(el->{
            el.setPatientId(JSON.toJSONString(userinfoService.getById(el.getPatientId())));
            el.setDoctorId(JSON.toJSONString(userinfoService.getById(el.getDoctorId())));
        });
        return RestBean.success(new PageInfo(list));
    }

    @RequestMapping("/create")
    public RestBean create(@RequestBody CaseInfo caseInfo, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        caseInfo.setDoctorId(uuid);
        caseInfo.setInputTime(new Date());
        return caseInfoService.save(caseInfo)?
                RestBean.success(caseInfo):RestBean.failure(500,"服务异常");
    }
//
//    @RequestMapping("add")
//    public RestBean create(@RequestBody CaseInfo caseInfo, HttpServletRequest request) {
//        String uuid = jwtUtil.getJwtUUID(request);
//        caseInfo.setDoctorId(uuid);
//        caseInfo.setInputTime(new Date());
//        return caseInfoService.save(caseInfo) ? RestBean.success(caseInfo) : RestBean.failure(500, "服务异常");
//    }

    @RequestMapping("listofmy")
    public RestBean listOfMy(HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUUID(request);
        List<CaseInfo> list = caseInfoService.list(new QueryWrapper<CaseInfo>().eq("patient_id", uuid).eq("is_deleted", 0).orderByDesc("input_time"));
        return RestBean.success(list);
    }

    @RequestMapping("/del/{caseId}")
    public RestBean del(@PathVariable String  caseId){
        try {
            CaseInfo byId = caseInfoService.getById(caseId);
            if(byId == null){
                throw new Exception();
            }
            byId.setIsDeleted(1);
            return caseInfoService.updateById(byId)?
                    RestBean.success(byId):RestBean.failure(500,"服务异常");
        }catch (Exception e){
            return RestBean.failure(400,"病例信息异常");
        }
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody CaseInfo caseInfo){
        try {
            CaseInfo byId = caseInfoService.getById(caseInfo.getCaseId());
            if(byId == null){
                throw new Exception();
            }
            byId.setComplaints(caseInfo.getComplaints());
            byId.setDoctorId(caseInfo.getDoctorId());
            byId.setInputTime(new Date());
            byId.setJws(caseInfo.getJws());
            byId.setPatientId(caseInfo.getPatientId());
            byId.setResults(caseInfo.getResults());
            byId.setXbs(byId.getXbs());
            return caseInfoService.updateById(byId)?
                    RestBean.success(byId):RestBean.failure(500,"服务异常");
        }catch (Exception e){
            return RestBean.failure(400,"病例信息异常");
        }
    }

}
